// pages/setup/setup.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectArray: [
      "嗨！这里有一个大红包！",
      "恭喜发财，大吉大利",
      "拼图实力赢红包"
    ],
    difficulty: [{
      "id": "1",
      "title": "3x3",
      "value": 3,
      "mask": "/images/3x3.png",
      "checked": true
    }, {
      "id": "2",
      "title": "4x4",
      "value": 4,
      "mask": "/images/4x4.png",
      "checked": false
    }, {
      "id": "3",
      "title": "5x5",
      "value": 5,
      "mask": "/images/5x5.png",
      "checked": false
    }],
    money: [{
      "id": "1",
      "value": 2,
      "checked": true
    }, {
      "id": "2",
      "value": 10,
      "checked": false
    }, {
      "id": "3",
      "value": 20,
      "checked": false
    }, {
      "id": "4",
      "value": 100,
      "checked": false
    }, {
      "id": "5",
      "value": "...",
      "checked": false
    }],
    isInput: false,
    currentTitle: "",
    currentDifficulty: null,
    currentMoney: 2,
    currnetAmount: 1,
    currentImages: "",
    customAmount:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this

    this.setData({
      currentTitle: self.data.selectArray[0],
      currentDifficulty: self.data.difficulty[0],
      currentMoney: self.data.money[0].value,
    })

    wx.showLoading({
      title: '加载中',
    })

    wx.downloadFile({
      url: options.images,
      success(res) {
        if (res.statusCode === 200) {
          self.setData({
            currentImages: res.tempFilePath
          })
        }
      },
      complete:function(res) {
        wx.hideLoading()
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  /**
   * 选择标题
   */
  onPickerChange: function(e) {
    let self = this
    let index = e.detail.value

    this.setData({
      currentTitle: self.data.selectArray[index]
    })
  },

  /**
   * 选择难度
   */
  radioChange: function(e) {
    let index = e.detail.value - 1
    let item = this.data.difficulty[index]

    this.setData({
      currentDifficulty: item
    })

  },
  /**
   * 选择金额
   */
  onClickMoney: function(e) {
    console.log(e)
    let index = e.currentTarget.dataset.value - 1
    let list = this.data.money
    let input = false

    list.forEach(function(v) {
      v.checked = false
    })

    list[index].checked = true 

    if (index == 4) {
      input = true
    }

    this.setData({
      money:list,
      isInput:input
    })
    
  },
  /**
   * 生成拼图红包
   */
  onClickGenerate: function(e) {
    let is = this
    let difficulty = this.data.currentDifficulty

    wx.reLaunch({
      url: '/pages/puzzle/puzzle?currentImages=' + is.data.currentImages + '&difficulty=' + is.data.currentDifficulty.value
    })
  },

  /**
   *  发启支付请求 
   */
  payRequest: function(e) {

  },
})